import React from "react";
import {WebPage} from "../../utils/WorkbenchDecoratorUtil";
import BaseComponent from "../../../../redare/components/BaseComponent";
import {getLoginUserInfo, isLoginUserInRootPlatform} from "../../utils/WorkbenchUtil";
import PlatformPresenter from "../../.gen/presenter/component/core/PlatformPresenter";
import RolePermissionManage from "../../components/permission/RolePermissionManage";
import {Tabs} from "antd";

export default @WebPage({
    module: '通用组件',
    classify: '角色权限',
    id: 'com.redare.webplatform.workbench.pages.role.RolePermissionPage',
    name: '角色权限管理',
    webPath: '/role/permission/manager',
    webFunIds: [
        'WebPlatformRolePermissionAddFun',
        'WebPlatformRolePermissionAddMenu',
        'WebPlatformRolePermissionDelFun',
        'WebPlatformRolePermissionDelMenu',
        'WebPlatformRoleModify',
        'WebPlatformRoleDel',
        'WebPlatformRoleAdd'
    ]
})
class RolePermissionPage extends BaseComponent {

    constructor(props) {
        super(props);
        this.presenter = new PlatformPresenter(this);
        this._isRootPlatform = isLoginUserInRootPlatform();
        this._user = getLoginUserInfo();
        if (!this._isRootPlatform) {
            this._form.platform = this._user.platform;
        }
    }

    componentDidMount() {
        if (this._isRootPlatform) {
            this.presenter.findPlatformList({enabled: true}, null, (result) => {
                let data = result.data;
                if (data.length > 0) {
                    if (data.length === 1) {
                        this._form.platform = data[0].platform;
                    } else {
                        this.setState({data});
                    }
                }
            });
        }
    }


    _renderTab() {
        let {data} = this.state;
        let tabs = [];
        data.forEach(platform => {
            tabs.push(
                <Tabs.TabPane tab={platform.name} key={`platform-${platform.platform}`}>
                    <div style={({padding: '0 10px'})}>
                        <RolePermissionManage platform={platform.platform}/>
                    </div>
                </Tabs.TabPane>
            );
        });
        return tabs;
    }

    render() {
        return (
            <>
                {
                    this._form.platform && (
                        <RolePermissionManage platform={this._form.platform}/>
                    )
                }
                {
                    this.state.data && (
                        <Tabs type="card">
                            {
                                this._renderTab()
                            }
                        </Tabs>
                    )
                }
            </>
        )
    }
}
